local Class = require('lua.lang.Class')
local OutputStream = require('lua.io.OutputStream')

--- @param pathOrFile string | File
--- @param append boolean
--- @return file
local function getLFile(pathOrFile, append)
	append = append or false
	return io.open(type(pathOrFile) == 'string' and pathOrFile or
			pathOrFile.pathname, append and 'ab' or 'wb')
end

--- @class FileOutputStream : OutputStream
--- @field file file
local FileOutputStream = Class {
	__extends = OutputStream
}

--- @param pathOrFile string | File
--- @param append boolean
--- @return FileOutputStream
function FileOutputStream:new(pathOrFile, append)
	--- @type FileOutputStream
	local o = {}
	o.file = getLFile(pathOrFile, append)
	return setmetatable(o, FileOutputStream)
end

--- @param i number
--- @return void
function FileOutputStream:write(i)
	local c = string.char(i)
	self.file:write(c)
end

--- @param s string
--- @return void
function FileOutputStream:writeString(s)
	self.file:write(s)
end

--- @return void
function FileOutputStream:close()
	self.file:close()
	self.file = nil
end

return FileOutputStream